<?php
/**
 * @file $HeadURL: imagecollective.php $
 * @author $Author: DongJC (dongjianchang@haibao.com) $
 * @date $Date: 2015-3-30 6:02:55 $
 * @brief 
 */
namespace haibao\cms\business\imagecollective;

use haibao\cms\business\BusinessException;

use \haibao\cms\data\imagecollective\ImageCollective as ImageCollectiveData;
use \haibao\cms\model\filter\imagecollective\ImageCollective as ImageCollectiveFilter;
use \haibao\cms\data\imagecollective\ImageCollectivePic as ImageCollectivePicData;
use \haibao\cms\model\filter\imagecollective\ImageCollectivePro as ImageCollectiveProFilter;
use \haibao\cms\model\filter\imagecollective\ImageCollectiveTag as ImageCollectiveTagFitler;
use \haibao\cms\model\filter\imagecollective\ImageCollectivePic as ImageCollectivePicFilter;  
use \haibao\cms\data\imagecollective\ImageCollectiveTag as ImageCollectiveTagData ;
use haibao\frame\data\query\Condition;
use \haibao\cms\model\filter\ImageLibrary as ImageLibraryFilter;

class ImageCollective extends \haibao\cms\business\Base{
	public static function searchIdsByKeywordFilter($filter){
		$data = new ImageCollectiveData();
		 
		return $data->searchIdsByKeywordFilter($filter);
	}
	
	public static function getBanners($parentTagId){
		if(!$parentTagId){
			throw new BusinessException('频道id不能为空');
		}
		
		$data = new ImageCollectiveData();
		
		return $data->getBanners($parentTagId);
	}
	
	public static function updateImageCollectiveClickNumber($id,$fromPc = true){
		if(!$id){
			throw new BusinessException('id不能为空');
		}
		$data = new ImageCollectiveData();
		$data->updateImageCollectiveClickNum($id,$fromPc);
	}
	
	/**
	 * 根据图集id获取点击量
	 */
	public static function getImageCollectiveClickNum($imageCollectiveId){
		if(!$imageCollectiveId){
			throw new BusinessException('图集Id不能为空');
		}
		$data = new ImageCollectiveData();
		return $data->getImageCollectiveClickNum($imageCollectiveId);
	}
	
	/**
	 * 根据图集id修改点击量
	 */
	public static function editImageCollectiveClickNum($imageCollectiveId,$editClickNum){
		if($editClickNum == ''){
			throw new BusinessException('点击数不能为空');
		}
		if(!$imageCollectiveId){
			throw new BusinessException('图集ID不能为空');
		}
		$data = new ImageCollectiveData();
		$data->editImageCollectiveClickNum($imageCollectiveId,$editClickNum);
	}
	
	public static function getRecommendEightImageCollective($imageSetId){
		$data = new ImageCollectiveData();
		
		return $data->getRecommendEightImageCollective($imageSetId);
	}
	
	public static function getRecommendCollectives($imageCollectiveId,$tagId){
		$data = new ImageCollectiveData();
		
		return $data->getRecommendCollectives($imageCollectiveId,$tagId);
	}
	
	public static function getSiblingImageCollectives($imageCollectiveId,$parentId){
		$data = new ImageCollectiveData();
		
		return $data->getSiblingImageCollectives($imageCollectiveId,$parentId);
	}
	
	/**
	 * 图库首页板块列表
	 * @param array $bannerIds
	 */
	public static function getIndexImageCollectiveByBannerIds($bannerIds){
		$data = new ImageCollectiveData();
		
		return $data->getIndexImageCollectiveByBannerIds($bannerIds);
	}
	
	public static function getImageCollectiveList($filterModel){
		$imageCollectiveDate = new ImageCollectiveData();
		return $imageCollectiveDate->getOne($filterModel);
	}	
	
	public static function updateImageCollective($model,$oldPublished = null){
		self::checkOperation(self::FUNC_CODE_IMAGECOLLECTIVE_EDIT);
		if(!$model->Id){
			throw new BusinessException('id不能为空');
		}
		$imageCollectiveDate = new ImageCollectiveData();
		$imageCollectiveDate->updateCollectiveDate($model,$oldPublished);
		self::saveOperateHistory(self::FUNC_CODE_IMAGECOLLECTIVE_EDIT, $model->toArray(false));
	}
	
	public static function addImageCollective($model){
		self::checkOperation(self::FUNC_CODE_IMAGECOLLECTIVE_ADD);		
		$imageCollectiveDate = new ImageCollectiveData();
		$imageCollectiveDate->imageCollectiveDate($model);
		self::saveOperateHistory(self::FUNC_CODE_IMAGECOLLECTIVE_ADD, $model->toArray(false));
	}
	
	public static function getLists($query){
		$imageCollectiveDate = new ImageCollectiveData();
		return $imageCollectiveDate->getAll($query);
	}
	
	public static function delImageCollective($id,$published = null){
		if(!$id){
			throw new BusinessException('图集id不能为空');
		}
		self::checkOperation(self::FUNC_CODE_IMAGECOLLECTIVE_DEL);
		$imageCollectiveDate = new ImageCollectiveData();
		$imageCollectiveDate->delImageCollectiveById($id,$published);
		self::saveOperateHistory(self::FUNC_CODE_IMAGECOLLECTIVE_DEL, array($id));
	}
	
	public static function addImageRelation($model){
		self::checkOperation(self::FUNC_CODE_IMAGECOLLECTIVE_ADD);
		$imageCollectiveDate = new ImageCollectiveData();	
		$imageCollectiveDate->saveImageCollectivePic($model);
		self::saveOperateHistory(self::FUNC_CODE_IMAGECOLLECTIVE_ADD, $model->toArray(false));
	}
	
	public static function addImageCollectivePro($model){
		self::checkOperation(self::FUNC_CODE_IMAGECOLLECTIVE_ADD);
		$imageCollectiveDate = new ImageCollectiveData();
		$imageCollectiveDate->saveImageCollectivePro($model);
		self::saveOperateHistory(self::FUNC_CODE_IMAGECOLLECTIVE_ADD, $model->ImageCollectivePro->toArray(false));
	}
	
	public static function haveImageCollectivePro($id){
		if(!$id) return false;
		$imageCollectiveDate = new ImageCollectiveData();
		return $imageCollectiveDate->haveImageCollectivePro($id);
	}
	
	public static function getImage($query){
		$imageCollectiveDate = new ImageCollectiveData();
		return $imageCollectiveDate->getAll($query);
	}
	/**
	 * 获得图集信息通过图集id
	 */
	public static function getImageColletiveById($id){
		if(!$id){
			throw new BusinessException('图集id不能为空');
		}
		$imageCollectiveDate = new ImageCollectiveData();
		$imageCollectiveFilter = new ImageCollectiveFilter();
		$imageCollectiveFilter->where(ImageCollectiveFilter::CONDITION_FIELD_NAME_ID, \haibao\frame\data\query\Condition::CONDITION_EQUAL, $id);
		return $imageCollectiveDate->getOne($imageCollectiveFilter);
	}
	
	public static function getImageCollectiveByFilter($filter){
		$imageCollectiveDate = new ImageCollectiveData();
		return $imageCollectiveDate->getAll($filter);
	}

	public static function delImageCollectivePic($id,$imageIds=null){
		if(!$id){
			throw new BusinessException('id不能为空');
		}
		$imageCollectiveData = new ImageCollectiveData();
		$imageCollectiveData->delImageCollectivePic($id,$imageIds);
		self::saveOperateHistory(self::FUNC_CODE_IMAGECOLLECTIVE_PIC_DEL, $id);
	}
	
	/**
	 * 得到图片描述
	 * @param unknown $query
	 */
	public static function getImageCollectivePicInfo($query){
		$imageCollectivePicData = new ImageCollectivePicData();
		return $imageCollectivePicData->getAll($query);
	}
	
	/**
	 * 保存图片描述（单张）
	 * @param  $model
	 */
	public static function updateImageCollectivePicInfo($model){
		if(!$model->ImageId){
			throw new BusinessException('图片id不能为空');
		}		
		$imageCollectivePicData = new ImageCollectivePicData();
		$imageCollectivePicData->saveImageCollectivePicInfo($model);
		self::saveOperateHistory(self::FUNC_CODE_IMAGECOLLECTIVE_PIC_INFO_UPDATE, $model->toArray(false));
	}
	
	/**
	 * 批量更新图片描述
	 * @param string $imageIdStr
	 * @param string $desc
	 * @param int $imageCollectiveId
	 */
	public static function updateImageCollectivePicInfoBatch($imageIdStr,$desc,$imageCollectiveId){
		if(!$imageIdStr){
			throw new BusinessException('图片id不能为空');
		}
		if(!$desc){
			throw new BusinessException('描述不能为空');
		}
		if(!$imageCollectiveId){
			throw new BusinessException('图集id不能为空');
		}
		$imageCollectiveData = new ImageCollectiveData();
		$imageCollectiveData->updateBatch($imageIdStr,$desc,$imageCollectiveId);
	}	
	
	//删除导读图
	public static function delProPicOne($imageId,$type,$imageCollectiveId){
		if(!$imageId){
			throw new BusinessException('图片id不能为空');
		}
		if(!$type){
			throw new BusinessException('图片类型不能为空');
		}
		if(!$imageCollectiveId){
			throw new BusinessException('图集id不能为空');
		}
		$imageCollectiveData = new ImageCollectiveData();
		$imageCollectiveData->delImageCollectivePro($imageId,$type,$imageCollectiveId);
		self::saveOperateHistory(self::FUNC_CODE_IMAGECOLLECTIVE_PRO_UPDATE, array($imageId));
	}

	public static function getClickCommentNumsByImgCollIds($imageCollectiveIds){
		$imageCollectiveData = new ImageCollectiveData();
		return $imageCollectiveData->getClickCommentNumsByImgCollIds($imageCollectiveIds);
	}
	
	public static function getImageColletiveByIds($imageCollectiveIds){
	    $imageCollectiveData = new ImageCollectiveData();
	    return $imageCollectiveData->getImageColletiveByIds($imageCollectiveIds);
	}
	
	public static function getImageCollectivesByIds($imageCollectiveIds){
	    $imageCollectiveData = new ImageCollectiveData();
	    return $imageCollectiveData->getImageCollectivesByIds($imageCollectiveIds);
	}
	
	public static function updateEditClickNumById($imageCollectiveId,$num){
		$imageCollectiveData = new ImageCollectiveData();
		$imageCollectiveData->updateEditClickNumById($imageCollectiveId, $num);
	}

	
	public static function hasImage($imageCollectiveId){
		if(!$imageCollectiveId){
			throw new BusinessException('图集id不能为空');
		}
		$imageCollectivePicData = new ImageCollectivePicData();
		return $imageCollectivePicData->hasImage($imageCollectiveId);
	}

	/**
	 *获取图集导读图
	 */
	public static function getProImageFigure($imageCollectiveIds,$type){
		$imageFigureIds = array();
		if($imageCollectiveIds){
			$imageCollectiveData = new ImageCollectiveData();
			$imageFigureIds = $imageCollectiveData->getProImageFigure($imageCollectiveIds,$type);
		}
		return $imageFigureIds;
	}
	/**
	 * 获取图集Level为0的导读图
	 */
	public static function getProImageLevelZero($collectiveId){
		$imageCollectiveData = new ImageCollectiveData();
		return $imageCollectiveData->getProImageLevelZero($collectiveId);
	}
	
	public static function getImagesByIds($imageCollectiveIds){
		$imageIds = array();
		if($imageCollectiveIds){
			$imageCollectiveData = new ImageCollectiveData();
			$imageIds = $imageCollectiveData->getImagesByIds($imageCollectiveIds);
		}
		return $imageIds;
	}

	
	public static function updateImageCollectiveStatus($imageCollectiveId,$status){
		$imageCollectiveData = new ImageCollectiveData();
		if(!$imageCollectiveId){
			throw new BusinessException('图集id不能为空');
		}
		$imageCollectiveData->updateImageCollectiveStatus($imageCollectiveId,$status);
	}
	
	/**
	 * 根据图集ID得主标签对应的父级标签ID
	 * @param unknown_type $imageCollectiveId
	 */
	public static function getParentTagIdByImageCollectiveId($imageCollectiveId){
		$imageCollectiveData = new ImageCollectiveData();
		if(!$imageCollectiveId){
			throw new BusinessException('图集id不能为空');
		}
		$imageCollectiveData->getParentTagIdByImageCollectiveId($imageCollectiveId);
	}
	
	/**
	 * 根据标签得到图集
	 * @param int $tagId 标签id
	 * @param 导读图类型  $proPicLevel
	 * @throws BusinessException
	 */
	public static function getImageColletiveByTag($tagId,$proPicLevel=0){
		if(!$tagId){
			throw new BusinessException('标签不能为空');
		}
		if($proPicLevel>0){
			$proPicLevels = array(0,$proPicLevel);
		}else{
			$proPicLevels = array(0);
		}
		$imageCollectiveData = new ImageCollectiveData();
		$imageCollectiveProFilter = new ImageCollectiveProFilter();
		$imageCollectiveFilter = new ImageCollectiveFilter();
		$imageCollectiveTagFitler = new ImageCollectiveTagFitler();
		//图集的属性
		$imageCollectiveFilter->select(array(
				ImageCollectiveFilter::CONDITION_FIELD_NAME_ID,
				ImageCollectiveFilter::CONDITION_FIELD_NAME_SHORT_TITLE,
				ImageCollectiveFilter::CONDITION_FIELD_NAME_TITLE,
				ImageCollectiveFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_PUBLISHTIME,
		));
		//图集导读图
		$imageCollectiveProFilter->select(array(
				ImageCollectiveProFilter::CONDITION_FIELD_NAME_ID,
				ImageCollectiveProFilter::CONDITION_FIELD_NAME_IMAGE_ID,
				ImageCollectiveProFilter::CONDITION_FIELD_NAME_IMAGE_IMAGE_TYPE
		));
		//图集标签
		$imageCollectiveTagFitler->select(array(
				ImageCollectiveTagFitler::CONDITION_FIELD_NAME_ID,
				ImageCollectiveTagFitler::CONDITION_FIELD_NAME_TAG_ID,
		));
		$imageCollectiveFilter->leftJoin($imageCollectiveProFilter,null,ImageCollectiveFilter::CONDITION_FIELD_NAME_ID,ImageCollectiveProFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_ID,ImageCollectiveFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_PRO);
		$imageCollectiveFilter->leftJoin($imageCollectiveTagFitler,null,ImageCollectiveFilter::CONDITION_FIELD_NAME_ID,ImageCollectiveTagFitler::CONDITION_FIELD_NAME_TAG_COLLECTIVE_ID,ImageCollectiveFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_TAG);
		$imageCollectiveFilter->where(ImageCollectiveFilter::CONDITION_FIELD_NAME_LEVEL, Condition::CONDITION_GREATER, 0);
		$imageCollectiveFilter->where(ImageCollectiveFilter::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL, \haibao\cms\model\data\imagecollective\ImageCollective::STATUS_ENABLE);
		 
		$imageCollectiveFilter->where(ImageCollectiveFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_PUBLISHED, Condition::CONDITION_EQUAL, \haibao\cms\model\data\imagecollective\ImageCollective::STATUS_YES_PUBLISH);
		
		$imageCollectiveTagFitler->where(ImageCollectiveTagFitler::CONDITION_FIELD_NAME_TAG_ID, Condition::CONDITION_EQUAL, $tagId);
		$imageCollectiveProFilter->where(ImageCollectiveProFilter::CONDITION_FIELD_NAME_IMAGE_IMAGE_TYPE, Condition::CONDITION_IN, $proPicLevels);
		$imageCollectiveFilter->order(ImageCollectiveFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_PUBLISHTIME, false);
		$imageCollectiveFilter->limit(1);
		
		return $imageCollectiveData->getAll($imageCollectiveFilter);
	}
	
	public static function setLevel($id,$level){
		if(!$id){
			throw new BusinessException('图集id不能为空');
		}		
		$imageCollectiveData = new ImageCollectiveData();
		$imageCollectiveData->setLevel($id, $level);
	}
	
	public static function delTag($query){
		$imageCollectiveTagData = new ImageCollectiveTagData();
		$imageCollectiveTagData->remove($query);		
		self::saveOperateHistory(self::FUNC_CODE_IMAGECOLLECTIVE_EDIT, $query);
	}
	
	public static function batchUpdateTag($model){
		$imageCollectiveData = new ImageCollectiveData();		
		$imageCollectiveData->batchUpdateTag($model);
	}
	
	public static function getImageCollectiveTagById($ids){
		$data = array();
		$imageCollectiveTagData = new ImageCollectiveTagData();
		$imageCollectiveTagFitler = new ImageCollectiveTagFitler();
		$imageCollectiveTagFitler->select(array(
				ImageCollectiveTagFitler::CONDITION_FIELD_NAME_TAG_COLLECTIVE_ID,
				ImageCollectiveTagFitler::CONDITION_FIELD_NAME_TAG_ID,
				ImageCollectiveTagFitler::CONDITION_FIELD_NAME_ID
		));
		$imageCollectiveTagFitler->where(ImageCollectiveTagFitler::CONDITION_FIELD_NAME_TAG_COLLECTIVE_ID,Condition::CONDITION_IN,$ids);
		$result=$imageCollectiveTagData->getAll($imageCollectiveTagFitler);
		if($result){
			foreach($result as $key => $value){
				$data[$value->ImageCollectiveId][$value->TagId] = $value->TagId;
			}
		}
		return $data;
	}
	/**
	 * 修改聚焦显示
	 */
	public static function setFocusShow($infoId,$imageIds,$infoType){
		$data = new \haibao\cms\data\ImageLibrary();
		return $data->setFocusShow($infoId,$imageIds,$infoType);
	}
	
	public static function editImageOrder($model){
		$imageCollectiveData = new ImageCollectiveData();
		$imageCollectiveData->editImageOrder($model);
	}
	
	public static function addChoiceImage($model){
	    $imageCollectiveData = new ImageCollectiveData();
	    $imageCollectiveData->addChoiceImage($model);
	}
	
	public static function getImageCollectivePicList($imageCollectiveId){
		$imageCollectivePicFilter = new ImageCollectivePicFilter();
		$imageLibraryFilter = new ImageLibraryFilter();
		$imageCollectivePicFilter->select(array(
				ImageCollectivePicFilter::CONDITION_FIELD_NAME_IMAGE_ID,
				ImageCollectivePicFilter::CONDITION_FIELD_NAME_IMAGE_DESCRIPTION,
				ImageCollectivePicFilter::CONDITION_FIELD_NAME_FOCUS_SHOW,
				ImageCollectivePicFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_ID,
				ImageCollectivePicFilter::CONDITION_FIELD_NAME_ID
		));
		$imageLibraryFilter->select(array(
				ImageLibraryFilter::CONDITION_FIELD_NAME_ID,
				ImageLibraryFilter::CONDITION_FIELD_NAME_FILENAME
		));
		$imageCollectivePicFilter->leftJoin($imageLibraryFilter,null,ImageCollectivePicFilter::CONDITION_FIELD_NAME_IMAGE_ID,ImageLibraryFilter::CONDITION_FIELD_NAME_ID,ImageCollectivePicFilter::CONDITION_FIELD_NAME_IMAGE_FILENAME,false);
		$imageCollectivePicFilter->where(ImageCollectivePicFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_ID, Condition::CONDITION_EQUAL, $imageCollectiveId);
		$imageCollectivePicData = new \haibao\cms\data\imagecollective\ImageCollectivePic();
		return $imageCollectivePicData->getAll($imageCollectivePicFilter);
	}
	
	public static function getStatisticList($querytArr,$isEditorSta){
		$imageCollectiveData = new ImageCollectiveData();
		return $imageCollectiveData->getStatisticList($querytArr,$isEditorSta);
	}
	
	public static function updateImageCollectiveCommentNum($imageCollectiveId,$num){
		if(!$imageCollectiveId){
			throw new BusinessException('图集id不能为空');
		}
		$imageCollectiveData = new ImageCollectiveData();
		$imageCollectiveData->updateImageCollectiveCommentNum($imageCollectiveId,$num);
	}
	
	public static function subtractCommentNum($imageCollectiveId){
		if(!$imageCollectiveId){
			throw new BusinessException('图集id不能为空');
		}
		$imageCollectiveData = new ImageCollectiveData();
		$imageCollectiveData->subtractCommentNum($imageCollectiveId);
	}
	
	public static function getPublishedImageCollectiveIds($imageCollectiveIds){
		$imageCollectiveData = new ImageCollectiveData();
		$imageCollectiveFilter = new ImageCollectiveFilter();
		$imageCollectiveFilter->select(array(ImageCollectiveFilter::CONDITION_FIELD_NAME_ID));
		$imageCollectiveFilter->where(ImageCollectiveFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_IN, $imageCollectiveIds);
		$imageCollectiveFilter->where(ImageCollectiveFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_PUBLISHED, Condition::CONDITION_EQUAL, \haibao\cms\model\data\imagecollective\ImageCollective::STATUS_YES_PUBLISH);
		$result = $imageCollectiveData->getAll($imageCollectiveFilter);
		$Ids = array();
		if($result){
			foreach($result as $imageCollective){
				array_push($Ids, $imageCollective->Id);
			}
		}
		return $Ids;
	}

	public static function updateTagTree($tagTreeArr){
		$tagData = new ImageCollectiveTagData();
		$tagData->updateTagTree(array_unique($tagTreeArr));
	}
}
